package com.bao.system.controller;


import com.bao.response.Result;
import com.bao.system.entity.BUser;
import com.bao.vo.UserVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author bao
 * @since 2021-06-03
 */
@RestController
@RequestMapping("/system/menu")
public class MenuController {


    @PostMapping("/python")
    public Result findUserPage(String str){
        System.out.println("start ------------------------");
        System.out.println(str);
        String data = runPython(str);
        System.out.println("end _____________________");
        return Result.ok().data("data", data);
    }



    //调用Python
    public String runPython(String strTemp) {
        String pythonPath = "D:\\workspace\\pythonproject\\pythonProject1\\BERT-BiLSTM-CRF-NER";
        String haha = "";
        //        String[] args = new String[]{"cmd","/k","start","python",pythonPath+"bert_lstm_ner.py"};
        try {
            FileOutputStream out=new FileOutputStream(new File(pythonPath+"/NERdata/test.txt"));
            BufferedOutputStream Buffered = new BufferedOutputStream(out);
            for (int i = 0; i < strTemp.length(); i++) {
                String temp ="\n";
                if(strTemp.charAt(i)!=' '){
                    temp=strTemp.charAt(i)+" O\n";
                }
                Buffered.write(temp.getBytes());
                System.out.println(temp);
            }
            Buffered.write("\n".getBytes());
            Buffered.flush();
            Buffered.close();
            File file=new File(pythonPath+"status.dat");
            FileWriter fileWriter =new FileWriter(file);
            fileWriter.write(48);
            fileWriter.flush();
            fileWriter.close();
            Runtime.getRuntime().exec("cmd /k start python D:\\workspace\\pythonproject\\pythonProject1\\BERT-BiLSTM-CRF-NER\\bert_lstm_ner.py");
            //            // 产生管道
            //            Process proc = Runtime.getRuntime().exec(args);
            //            // 必须写在 waitFor 之前）
            //            System.out.println(1);
            //            BufferedReader br = new BufferedReader(new InputStreamReader(proc.getInputStream()));
            //
            //            System.out.println(2);
            //            br.read();
            ////            String s;
            ////            while((s=br.read())!=null){
            ////                System.out.println(s);
            ////            }
            //            System.out.println(3);
            //            //等待执行完毕
            //            int retCode = proc.waitFor();
            //            System.out.println(4);
            //            if(retCode == 0){
            //                System.out.println("Python正常执行结束");
            //            }
            for(int i=0;i<300;i++){
                TimeUnit.SECONDS.sleep(1);
                InputStream input = new FileInputStream(file);
                int tempByte= input.read();
                if(tempByte != -1&&tempByte==49){
                    System.out.println("Python正常执行结束");
                    break;
                }
                input.close();
            }
            fileWriter =new FileWriter(file);
            fileWriter.write(48);
            fileWriter.flush();
            fileWriter.close();

            FileReader fileReader = new FileReader(pythonPath+"\\output\\label_test.txt");
            BufferedReader br = new BufferedReader(fileReader);
            haha =productHTML(br);
            System.out.println("br------------------------");
            System.out.println(br);
            System.out.println("br end------------------------");
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
        return haha;
    }




    //已处理文本生产html
    public String productHTML(BufferedReader br) throws IOException {
        List<String> strList = new ArrayList<>();
        String data = "";
        int num = 0;
        String line;
        // 一次读入一行数据
        System.out.println("内容--------------");
        boolean flagO = true;
        boolean flagB = false;
        boolean flagI = false;
        while ((line = br.readLine()) != null) {
            if(line != ""){
                System.out.println(line);
                /*判断是不是B*/
                if(line.indexOf(" O B-") != -1){
                    if(flagI){
                        data = data + "</span>" + "<span style='color: blue'>"+ line.substring(0, 1);
                    }else if(flagO){
                        data = data + "<span style='color: blue'>"+ line.substring(0, 1);
                    }else if (flagB){
                        data = data + "</span>" + "<span style='color: blue'>"+ line.substring(0, 1);
                    }
                    flagB = true;
                    flagO = false;
                    flagI = false;
                }
                /*判断是不是I*/
                else if(line.indexOf(" O I-") != -1){
                    if(!flagO){
                        data = data + line.substring(0, 1);
                        flagI = true;
                        flagB = false;
                        flagO = false;
                    }
                    else {
                        data = data + line.substring(0, 1);
                    }
                }
                /*判断是不是O*/
                else if(line.indexOf(" O O") != -1){
                    if(flagO){
                        data = data + line.substring(0, 1);
                    } else {
                        data = data + "</span>" + line.substring(0, 1);
                    }
                    flagI = false;
                    flagB = false;
                    flagO = true;
                }
            }

        }
        System.out.println(data);
        System.out.println("内容   end--------------");

//        strTemp.append(dealS(flagA, 7, ""));
        return data;
    }






}

